package com.lizicloud.infrastructure.common.utils;

import com.lizicloud.infrastructure.common.constant.AppConstants;
import com.lizicloud.infrastructure.common.exception.BusinessException;

/**
 * 参数校验工具类
 */
public class ValidationUtils {
    
    /**
     * 默认错误码
     */
    private static final String DEFAULT_ERROR_CODE = "VALIDATION_ERROR";
    
    /**
     * 校验对象是否为空
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    /**
     * 校验字符串是否为空
     */
    public static void notEmpty(String str, String message) {
        if (StringUtils.isEmpty(str)) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    /**
     * 校验正数
     */
    public static void positive(Number number, String message) {
        if (number == null || number.doubleValue() <= 0) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    /**
     * 校验非负数
     */
    public static void nonNegative(Number number, String message) {
        if (number == null || number.doubleValue() < 0) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    /**
     * 校验邮箱格式
     */
    public static void isEmail(String email, String message) {
        if (!StringUtils.isEmail(email)) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    /**
     * 校验手机号格式
     */
    public static void isMobile(String mobile, String message) {
        if (!StringUtils.isMobile(mobile)) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    /**
     * 校验字符串长度范围
     */
    public static void lengthRange(String str, int min, int max, String message) {
        if (str == null) {
            if (min > 0) {
                throw new BusinessException(DEFAULT_ERROR_CODE, message);
            }
            return;
        }
        int length = str.length();
        if (length < min || length > max) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    /**
     * 校验数值范围
     */
    public static void range(Number number, double min, double max, String message) {
        if (number == null) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
        double value = number.doubleValue();
        if (value < min || value > max) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    /**
     * 校验条件是否为真
     */
    public static void isTrue(boolean condition, String message) {
        if (!condition) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    /**
     * 校验条件是否为假
     */
    public static void isFalse(boolean condition, String message) {
        if (condition) {
            throw new BusinessException(DEFAULT_ERROR_CODE, message);
        }
    }
    
    private ValidationUtils() {
        // 私有构造函数，防止实例化
    }
}